// ignore_for_file: library_private_types_in_public_api

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:verse_systems/widgets/easy_popup/easy_popup.dart';
import 'package:verse_systems/widgets/easy_popup/easy_popup_child.dart';
import 'package:flutter/material.dart';

class DropDownMenu extends StatefulWidget with EasyPopupChild {
  final Function(dynamic model) onPressed;
  final double top;
  DropDownMenu({Key? key, required this.onPressed, this.top = 50.0})
      : super(key: key);

  final _PopController controller = _PopController();

  @override
  _DropDownMenuState createState() => _DropDownMenuState();

  @override
  dismiss() {
    controller.dismiss();
  }
}

class _DropDownMenuState extends State<DropDownMenu>
    with SingleTickerProviderStateMixin {
  late Animation<Offset> _animation;
  late AnimationController _controller;

  final List voList = [
    {"title": "按时间排序"},
    {"title": "按类别排序"},
  ];

  @override
  void initState() {
    super.initState();
    widget.controller._bindState(this);
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 200),
    );
    _animation = Tween<Offset>(begin: const Offset(0, -1), end: Offset.zero)
        .animate(_controller);
    _controller.forward();
  }

  dismiss() {
    _controller.reverse();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Container(
          padding: EdgeInsets.only(
              top: MediaQuery.of(context).padding.top + widget.top),
          constraints: const BoxConstraints(maxHeight: 500, minHeight: 100),
          child: ClipRect(
            child: SlideTransition(
              position: _animation,
              child: Container(
                // margin: EdgeInsets.only(top: -5),
                color: Colors.white,
                // decoration: BoxDecoration(
                //     color: Colors.white,
                //     borderRadius: BorderRadius.circular(10.w)),
                child: ListView.builder(
                  padding: const EdgeInsets.all(0),
                  shrinkWrap: true,
                  itemCount: voList.length,
                  itemExtent: 50,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        EasyPopup.pop(context);
                        widget.onPressed(voList[index]["title"]);
                      },
                      child: Container(
                        alignment: Alignment.center,
                        child: Text(
                          voList[index]["title"],
                          style: TextStyle(
                            fontSize: 15.sp,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ),
            ),
          ),
        ));
  }
}

class _PopController {
  late _DropDownMenuState state;

  _bindState(_DropDownMenuState state) {
    this.state = state;
  }

  dismiss() {
    state.dismiss();
  }
}
